package 二叉树.练习;

import 二叉树.基础.TreeNode;
//另一颗树的子树
public class Text_2 {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) {
            return true;
        }
        else if(p == null || q == null) {
            return false;
        }
        //不能写返回true，不能因为一次满足直接返回结果为true
        else if(p.val != q.val) {
            return false;
        }
        else {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }

    public boolean isSubtree(TreeNode root,TreeNode subRoot){
        if(root == null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }
        boolean b1 =  isSubtree(root.left,subRoot);
        if(b1 == true){
            return true;
        }
        boolean b2 = isSubtree(root.right,subRoot);
        if(b2 == true){
            return true;
        }
        return false;
    }
}
